package com.kasax.imgloader.demo.utils;

import android.content.Context;
import android.os.Environment;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.nostra13.universalimageloader.BuildConfig;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.io.File;

public class ImageUtils {
    private static DisplayImageOptions options;

    static {

    }

    public static void loadCircleImageWithGlide(ImageView imageView, String url) {
        Glide.with(imageView.getContext()).load(url).transforms(new CenterCrop(), new RoundedCorners(UiUtils.dp2px(imageView.getContext(), 12))).into(imageView);
    }

    public static void initImageLoader(Context context) {
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
        config.threadPriority(Thread.NORM_PRIORITY - 2);
        config.denyCacheImageMultipleSizesInMemory();
        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
        config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
        config.tasksProcessingOrder(QueueProcessingType.LIFO);
        config.diskCache(new UnlimitedDiskCache(new File(Environment.getExternalStorageDirectory() + "/target")));

        if (BuildConfig.DEBUG) {
            config.writeDebugLogs(); // 发布release包时，移除log信息     }
        }
        //  用configuration初始化ImageLoader
        ImageLoader.getInstance().init(config.build());
    }

    public static void loadCircleImageWithImgLoader(ImageView imageView, String url) {
        options = new DisplayImageOptions.Builder()
                .displayer(new RoundedBitmapDisplayer(UiUtils.dp2px(imageView.getContext(), 12)))
                .build();
        ImageLoader.getInstance().displayImage(url, imageView, options);
    }


}
